/**
 * 
 */
package com.moyq5.mvcmaker.impl;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.Id;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.moyq5.mvcmaker.MvcMaker;
import com.moyq5.mvcmaker.WebModule;
import com.moyq5.mvcmaker.freemarker.FreeMarkerFactory;

import freemarker.core.ParseException;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;


/**
 * Json字段筛选类生成器
 * @author Moyq5
 * @date 2016年9月27日
 */
public class JacksonFilterMvcMaker implements MvcMaker {

	private static Logger log = LoggerFactory.getLogger(JacksonFilterMvcMaker.class);
	
	/* 生成的文件名后缀 */
	private static final String SUFFIX = "Filter.java";
	/* 模板文件名 */
	private static final String FTL = "filter.ftl";
	
	private static Set<Class<?>> fitlerClasses = new HashSet<Class<?>>();
	static {
		fitlerClasses.add(String.class);
		fitlerClasses.add(Boolean.class);
		fitlerClasses.add(Integer.class);
		fitlerClasses.add(Long.class);
		fitlerClasses.add(Double.class);
		fitlerClasses.add(Float.class);
		fitlerClasses.add(BigDecimal.class);
		fitlerClasses.add(Date.class);
	}
	
	private WebModule module;
	public JacksonFilterMvcMaker(WebModule module) {
		this.module = module;
	}

	@Override
	public void execute(String entityName) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		String fileName = entityName + SUFFIX;
		String filePath = module.getPath() + File.separator + (module.getJacksonFilterPackage().replace(".", File.separator));
		File destFile = new File(filePath, fileName);

		log.debug("{}Json字段筛选类：{}.{}", 
				new Object[]{destFile.exists() ? "已存在": "创建", module.getJacksonFilterPackage(), fileName});
		
		if (!destFile.exists()) {
			destFile.getParentFile().mkdirs();
			FileWriter fw = new FileWriter(destFile);
			Template temp = FreeMarkerFactory.getCfg().getTemplate(FTL);
			
			String lowerName = entityName.substring(0, 1).toLowerCase() + entityName.substring(1);
			String idName = "";
			List<String> fieldNames = new ArrayList<String>();
			try {
				Field[] fields = Class.forName(module.project().beanModule().getEntityPackage() + "." + entityName).getDeclaredFields();
				for (Field field : fields) {
					if (field.isAnnotationPresent(Id.class)) {
						idName = field.getName();
					}
					if (fitlerClasses.contains(field.getType()) || field.getType().isEnum()) {
						fieldNames.add(field.getName());
					}
				}
			} catch (SecurityException | ClassNotFoundException e) {
				e.printStackTrace();
			}
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("entityName", entityName);// 类名
			model.put("module", module); // 配置信息
			model.put("beanModule", module.project().beanModule()); // Bean配置信息
			model.put("lowerName", lowerName); 	// 首字母小写类名
			model.put("idName", idName);// 主键字段
			model.put("fieldNames", fieldNames);// 基本字段
			temp.process(model, fw);
		}
		
	}
	

}
